package com.zhangc.blog.admin.web.controller.admin;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangc.blog.admin.entity.BlogCommentEntity;
import com.zhangc.blog.admin.service.BlogCommentServiceImpl;
import com.zhangc.blog.admin.web.dto.AjaxPutPage;
import com.zhangc.blog.admin.web.dto.AjaxResultPage;
import com.zhangc.blog.base.ResultBO;

/**
 * @qq交流群 796794009
 * @qq 1320291471
 * @Description: 评论标签
 * @date: 2019/8/6 17:24
 */
@Controller
@RequestMapping("/admin")
public class CommentController {

    @Resource
    private BlogCommentServiceImpl blogCommentService;

    @GetMapping("/v1/comment")
    public String gotoComment() {
        return "adminLayui/comment-list";
    }

    /**
     * 返回评论列表
     *
     * @param ajaxPutPage
     * @param condition
     * @return com.site.blog.pojo.dto.AjaxResultPage<com.site.blog.entity.BlogComment>
     * @date 2020/4/24 21:23
     */
    @ResponseBody
    @GetMapping("/v1/comment/paging")
    public AjaxResultPage<BlogCommentEntity> getCommentList(AjaxPutPage<BlogCommentEntity> ajaxPutPage, BlogCommentEntity condition) {
        QueryWrapper<BlogCommentEntity> queryWrapper = new QueryWrapper<>(condition);
        Page<BlogCommentEntity> page = ajaxPutPage.putPageToPage();
        blogCommentService.page(page, queryWrapper);
        AjaxResultPage<BlogCommentEntity> result = new AjaxResultPage<>();
        result.setData(page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    /**
     * 修改评论状态
     *
     * @param blogComment
     * @return com.site.blog.pojo.dto.Result<java.lang.String>
     * @date 2020/4/24 21:21
     */
    @ResponseBody
    @PostMapping(value = { "/v1/comment/isDel", "/v1/comment/commentStatus" })
    public ResultBO<String> updateCommentStatus(BlogCommentEntity blogComment) {
        boolean flag = blogCommentService.updateById(blogComment);
        if (flag) {
            return ResultBO.success();
        }
        return ResultBO.error500();
    }

    /**
     * 删除评论
     *
     * @param commentId
     * @return com.site.blog.pojo.dto.Result<java.lang.String>
     * @date 2020/4/24 21:23
     */
    @ResponseBody
    @PostMapping("/v1/comment/delete")
    public ResultBO<String> deleteComment(@RequestParam Long commentId) {
        boolean flag = blogCommentService.removeById(commentId);
        if (flag) {
            return ResultBO.success();
        }
        return ResultBO.error500();
    }

    /**
     * 编辑评论
     *
     * @param blogComment
     * @return com.site.blog.pojo.dto.Result<java.lang.String>
     * @date 2020/4/24 21:21
     */
    @ResponseBody
    @PostMapping("/v1/comment/edit")
    public ResultBO<String> editComment(BlogCommentEntity blogComment) {
        blogComment.setReplyCreateTime(new Date());
        blogComment.setCommentBody(StringEscapeUtils.escapeHtml4(blogComment.getCommentBody()));
        boolean flag = blogCommentService.updateById(blogComment);
        if (flag) {
            return ResultBO.success();
        } else {
            return ResultBO.error500();
        }
    }

}
